/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package service

import (
	"context"

	"gitee.com/WisdomClassroom/core"
	"gitee.com/WisdomClassroom/core/models"
	"gitee.com/WisdomClassroom/core/protobufs/v1/pb"
	"gitee.com/WisdomClassroom/question/glb"
	"google.golang.org/grpc/metadata"
)

func (s *Service) ListWrongQuestionAnswers(ctx context.Context, request *pb.Empty) (*pb.ListWrongQuestionAnswersResponse, error) {
	md, ok := metadata.FromIncomingContext(ctx)
	if !ok {
		glb.Logger.Error("no metadata")
	}
	token, err := core.UnpackTokenFromMetadata(md)
	if err != nil {
		return &pb.ListWrongQuestionAnswersResponse{Status: &pb.ResponseStatus{
			Code: core.ResponseStatusCodeNotAuth, Message: err.Error(),
		}}, nil
	}

	if token.Type != core.UserTypeStudent {
		return &pb.ListWrongQuestionAnswersResponse{Status: &pb.ResponseStatus{
			Code:    core.ResponseStatusCodeNotAuth,
			Message: "仅学生能调用此接口",
		}}, nil
	}

	questionAnswers := make([]models.QuestionAnswer, 0, 32)
	err = glb.DB.Preload("Questions").Find(&questionAnswers, "student_id = ?", token.AccountID).Error
	if err != nil {
		glb.Logger.Error(err.Error())
		return listWrongQuestionAnswersRespSvrErr, nil
	}

	questionsResp := make([]*pb.ListWrongQuestionAnswersResponse_QuestionBasicInfoAndAnswer, 0, len(questionAnswers))
	for _, questionAnswer := range questionAnswers {
		questionsResp = append(questionsResp, &pb.ListWrongQuestionAnswersResponse_QuestionBasicInfoAndAnswer{
			Question: &pb.QuestionBasicInfo{
				UUID:     questionAnswer.Question.UUID,
				Question: questionAnswer.Question.Question.String,
				Analysis: questionAnswer.Question.Analysis.String,
				Answers: &pb.QuestionAnswers{
					AnswerA: questionAnswer.Question.AnswerA.String,
					AnswerB: questionAnswer.Question.AnswerB.String,
					AnswerC: questionAnswer.Question.AnswerC.String,
					AnswerD: questionAnswer.Question.AnswerD.String,
					AnswerE: questionAnswer.Question.AnswerE.String,
					AnswerF: questionAnswer.Question.AnswerF.String,
					AnswerG: questionAnswer.Question.AnswerG.String,
					AnswerH: questionAnswer.Question.AnswerH.String,
				},
				Options: &pb.QuestionOptions{
					IsA:    questionAnswer.Question.IsA.Bool,
					IsB:    questionAnswer.Question.IsB.Bool,
					IsC:    questionAnswer.Question.IsC.Bool,
					IsD:    questionAnswer.Question.IsD.Bool,
					IsE:    questionAnswer.Question.IsE.Bool,
					IsF:    questionAnswer.Question.IsF.Bool,
					IsG:    questionAnswer.Question.IsG.Bool,
					IsH:    questionAnswer.Question.IsH.Bool,
					IsTrue: questionAnswer.Question.IsTrue.Bool,
				},
				Type: questionAnswer.Question.Type.Int32,
			},
			IsCorrect: questionAnswer.IsCorrect,
		})
	}

	return &pb.ListWrongQuestionAnswersResponse{
		Status:    &pb.ResponseStatus{Code: core.ResponseStatusCodeSuccess},
		Questions: questionsResp,
	}, nil
}
